﻿using System;
using Microsoft.Practices.ObjectBuilder;
using WCSFContrib.ECommerceCatalog.BusinessEntities;
using System.Globalization;

namespace WCSFContrib.ECommerceCatalog.Product.Views
{
    public partial class ProductDetails : Microsoft.Practices.CompositeWeb.Web.UI.UserControl, IProductDetailsView
    {
        private ProductDetailsPresenter _presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this._presenter.OnViewInitialized();
            }
            this._presenter.OnViewLoaded();
        }

        [CreateNew]
        public ProductDetailsPresenter Presenter
        {
            set
            {
                this._presenter = value;
                this._presenter.View = this;
            }
        }

        #region IProductDetailsView Members

        public void ShowProductDetails(Item item)
        {
            if (item == null)
                return;

            _presenter.OnShowProductDetails(item);
        }

        public int ItemId
        {
            get { return int.Parse(IdLabel.Text, CultureInfo.CurrentCulture); }
            set { IdLabel.Text = String.Format(CultureInfo.CurrentCulture, "{0}", value); }
        }

        public string ItemImage
        {
            set { ProductImage.ImageUrl = value; }
        }

        public string ItemName
        {
            set { NameLabel.Text = value; }
        }

        public double ItemPrice
        {
            set { PriceLabel.Text = String.Format(CultureInfo.CurrentCulture, "$ {0}", value); }
        }

        public int ItemQuantity
        {
            get { return int.Parse(QuantityLabel.Text, CultureInfo.CurrentCulture); }
            set { QuantityLabel.Text = String.Format(CultureInfo.CurrentCulture, "{0}", value); }
        }

        public Category ItemCategory
        {
            set
            {
                Category category;
                category = value;
                CategoryLabel.Text = category.Name;

                while (category.ParentCategory != null)
                {
                    CategoryLabel.Text = category.ParentCategory.Name + " > " + CategoryLabel.Text;
                    category = category.ParentCategory;
                }
            }
        }

        public string ItemDescription
        {
            set { DescriptionLabel.Text = value; }
        }

        #endregion
    }
}

